package com.example.animation;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";
    private ImageView fpv;
    private ImageView fpvTop;
    private ImageView map;
    private RelativeLayout content;


    private int deviceHeight;
    private int deviceWidth;
    private int mapHeight;
    private int mapWidth;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN); // 隐藏状态栏
        setContentView(R.layout.activity_main);


        content = (RelativeLayout)findViewById(R.id.content);

        fpv = (ImageView)findViewById(R.id.fpv);
        fpv.setOnClickListener(this::onClick);

        map = (ImageView)findViewById(R.id.map);
        map.setOnClickListener(this::onClick);

        fpvTop = (ImageView)findViewById(R.id.fpv_top);
        fpvTop.setOnClickListener(this::onClick);

    }


    private final int SHOW_FPV = 0x10;
    private Handler mHandler = new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case SHOW_FPV:

                    break;
            }
            super.handleMessage(msg);
        }
    };

    private void getDeviceWidthAndHeight() {
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        deviceHeight = displayMetrics.heightPixels;
        deviceWidth = displayMetrics.widthPixels;
        mapWidth = DensityUtil.dip2px(150,this);
        mapHeight = DensityUtil.dip2px(100,this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.map:

                ScaleAnimation mapScaleAnimation = new ScaleAnimation(1.0f,5.447942f,1.0f,3.440000f,Animation.RELATIVE_TO_SELF,1.0f,Animation.RELATIVE_TO_SELF,1.0f);
                mapScaleAnimation.setDuration(500);
                mapScaleAnimation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        changSize(map,deviceWidth,deviceHeight);
                        fpv.setVisibility(View.INVISIBLE);
                        fpvTop.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                map.startAnimation(mapScaleAnimation);
                break;
            case R.id.fpv_top:
                ScaleAnimation fpvScaleAnimation = new ScaleAnimation(1.0f,5.447942f,1.0f,3.440000f,Animation.RELATIVE_TO_SELF,1.0f,Animation.RELATIVE_TO_SELF,1.0f);
                fpvScaleAnimation.setDuration(500);
                fpvScaleAnimation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        changSize(fpvTop,deviceWidth,deviceHeight);
                        changSize(map,mapWidth,mapHeight);
                        fpv.setVisibility(View.VISIBLE);
                        fpvTop.setVisibility(View.INVISIBLE);
                        changSize(fpvTop,mapWidth,mapHeight);

                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                fpvTop.startAnimation(fpvScaleAnimation);
                break;
        }
    }

    private void changSize(View view,int width,int height){

        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
        layoutParams.width = width;
        layoutParams.height = height;
        view.setLayoutParams(layoutParams);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getDeviceWidthAndHeight();
    }





}